﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Runtime.Serialization.Json;
using System.IO;
using System.Text;

namespace Howork
{
    public partial class QueryDevice : System.Web.UI.Page
    {
        protected string GetJson<T>(T obj)
        {
            DataContractJsonSerializer jsonSerializer = new DataContractJsonSerializer(obj.GetType());
            using (MemoryStream stream = new MemoryStream())
            {
                jsonSerializer.WriteObject(stream, obj);
                string jsonString = Encoding.UTF8.GetString(stream.ToArray());
                return jsonString;
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            String type = Request.QueryString["typedevice"];
            List<DeviceInfo> devicelist;
            string jsonString;
            if (type == "state")
            {
                DeviceManage dm = new DeviceManage();
                devicelist = dm.GetDeviceList();
                jsonString = GetJson<List<DeviceInfo>>(devicelist);
                Response.Write(jsonString);
                Response.End();

                return;
            }
            else if (type == "delete")
            {
                DeviceManage dm = new DeviceManage();
                int device_id = Convert.ToInt32(Request["device_id"]);
                bool result = dm.DeleteDevice(device_id);
                if (result == true)
                {
                    string sresult = "删除设备成功";
                    jsonString = GetJson<string>(sresult);
                }
                else
                {
                    string sresult = "删除设备失败";
                    jsonString = GetJson<string>(sresult);
                }
                Response.Write(jsonString);
                Response.End();
                return;
            }
            else if (type == "adddevice")
            {
                DeviceManage dm = new DeviceManage();
                string device_name = Request["device_name"];
                int type_id = Convert.ToInt32(Request["type_id"]);
                string type_name = Request["name"];
                string result;
                if (type_id == -1)
                {

                    List<DeviceTypeInfo> type_list = dm.GetDeviceType();
                    for (int i = 0; i < type_list.Count; i++)
                    {
                        if (type_name == type_list[i].name)
                        {
                            result = "该设备类型已经存在";
                            jsonString = GetJson<string>(result);
                            Response.Write(jsonString);
                            Response.End();
                            return;
                        }
                    }



                    type_id = dm.AddType(type_name);
                    
                    if(type_id<0)
                    {
                         result = "添加新设备类型失败";
                         jsonString = GetJson<string>(result);
                         Response.Write(jsonString);
                         Response.End();
                         return;
                       
                    }
                }
                if (dm.AddDevice(device_name, type_id))
                {
                     result = "添加设备成功";
                    
                }
                else
                {
                    result = "添加设备失败";
                   
                }
                jsonString = GetJson<string>(result);
                Response.Write(jsonString);
                Response.End();
                return;
            }
            
            else if (type == "gettype")
            {
                DeviceManage dm = new DeviceManage();
                List<DeviceTypeInfo> device_type = dm.GetDeviceType();

                jsonString = GetJson<List<DeviceTypeInfo>>(device_type);
                Response.Write(jsonString);
                Response.End();
            }
            string begin_time = Request["begin"];
            string end_time = Request["end"];
            int device_tpye =Convert.ToInt32(Request["type"]);
            DeviceManage dManage = new DeviceManage();
            

            DateTime dt_begin = DateTime.Parse(begin_time);
            DateTime dt_end = DateTime.Parse(end_time);
            devicelist = dManage.GetFreeDeviceList(device_tpye,dt_begin ,dt_end );
            
            jsonString = GetJson<List<DeviceInfo>>(devicelist);
            Response.Write(jsonString);
            Response.End();
        }
    }
}